<?php
/**
 * amazon(亚马逊)平台账号
 * (model)
 */

namespace App\Models\Basics;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class SysPlatAmazonAccount extends Model {

    protected $connection = 'mysql';

    protected $table = 'b_sys_plat_amazon_account';

    protected $guarded = [];

    //关联平台账号
    public function plat_account(){
        return $this->belongsTo(SysPlatAccount::class, 'account_id');
    }

    //临时。切换后无需再用
    public function amazon_area()
    {
        return $this->belongsTo(AmazonArea::class,'area_id');
    }

    public function account_country()
    {
        return $this->belongsToMany(AmazonCountry::class,
            'b_amazon_account_country',
            'account_id',
            'country_id',
            'id',
            'country_id');
    }

    public static function options($where = [])
    {
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->pluck('account','id');
    }

    public static function getAccountById($id){
        return self::query()->where('id',$id)->value('account');
    }

    public static function getIdByAccount($account){
        return self::query()->where('account',$account)->value('id');
    }

    ////账号关联的所有国家
    //public static function getCountriesById($id){
    //    //return DB::table('b_amazon_account_country')->where('account_id',$id)->pluck('country_id');
    //
    //    return AmazonAccountCountry::query()->where('account_id',$id)->pluck('country_id');
    //}

    public static function optionsForApi($where = [])
    {
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        return $query->where('status',1)->get();
    }

}
